package com.atguigu.yygh.hosp.controller.api;

import com.alibaba.excel.util.StringUtils;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.hosp.util.HospApiUtil;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;

import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName ApiController
 * @Description: 类描述
 * @Author: admin
 * @CreateDate: 2024/6/3 16:29
 * @UpdateUser: 更新人
 * @UpdateDate: 2024/6/3 16:29
 * @UpdateRemark: 更新的信息
 * @Version: 1.0
 */


@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 用于删除排班信息
     * @param request
     * @return
     */
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramsMap = HospApiUtil.hospStringToObject(request, hospitalSetService);

        String hoscode = (String)paramsMap.get("hoscode");
        String hosScheduleId = (String)paramsMap.get("hosScheduleId");

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

    /**
     * 用于获取排班信息
     * @param request
     * @return
     */
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        Map<String, Object> paramsMap = HospApiUtil.hospStringToObject(request, hospitalSetService);
        //获取hoscode
        String hoscode = (String)paramsMap.get("hoscode");
        String depcode = (String)paramsMap.get("depcode");
        //判断是否有page参数，如果没有则默认查询第一页
        Integer page = StringUtils.isEmpty(paramsMap.get("page"))? 1 : Integer.parseInt((String) paramsMap.get("page"));
        Integer limit = StringUtils.isEmpty(paramsMap.get("limit"))? 10 : Integer.parseInt((String) paramsMap.get("limit"));


        //使用实体类用来存放查询条件
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        //调用service层方法获取科室列表
        Page<Schedule> pageModel =scheduleService.findPageSchedule(scheduleQueryVo, page, limit);
        return Result.ok(pageModel);
    }

    /**
     * 用于上传排班信息
     * @param request
     * @return
     */
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> paramsMap = HospApiUtil.hospStringToObject(request, hospitalSetService);
        scheduleService.save(paramsMap);
        return Result.ok();
    }


    /**
     * 用于删除科室信息
     * @param request
     * @return
     */
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramsMap = HospApiUtil.hospStringToObject(request, hospitalSetService);

        String hoscode = (String)paramsMap.get("hoscode");
        String deptcode = (String)paramsMap.get("depcode");

        departmentService.remove(hoscode, deptcode);
        return Result.ok();
    }


    /**
     * 用于获取科室列表
     * @param request
     * @return
     */
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        Map<String, Object> paramsMap = HospApiUtil.hospStringToObject(request, hospitalSetService);

        //获取hoscode
        String hoscode = (String)paramsMap.get("hoscode");
        //判断是否有page参数，如果没有则默认查询第一页
        Integer page = StringUtils.isEmpty(paramsMap.get("page"))? 1 : Integer.parseInt((String) paramsMap.get("page"));
        Integer limit = StringUtils.isEmpty(paramsMap.get("limit"))? 10 : Integer.parseInt((String) paramsMap.get("limit"));


        //使用实体类用来存放查询条件
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service层方法获取科室列表
        Page<Department> pageModel =departmentService.findPageDepartment(departmentQueryVo, page, limit);
        return Result.ok(pageModel);
    }

    /**
     * 添加获取科室信息
     * @param request
     * @return
     */
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> paramsMap = HospApiUtil.hospStringToObject(request, hospitalSetService);

        //调用service层方法保存科室信息
        departmentService.save(paramsMap);

        return Result.ok();
    }


    /**
     * 用于获取医院信息
     * @param request
     * @return
     */
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //进行签名验证和参数转换
        Map<String, Object> paramsMap = HospApiUtil.hospStringToObject(request, hospitalSetService);
        String hoscode = (String)paramsMap.get("hoscode");

        //调用service层方法获取医院信息
        Hospital hospital =hospitalService.getByHospcode(hoscode);
        return Result.ok(hospital);
    }



    /**
     * 用于添加医院信息
     * @param request
     * @return
     */
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
//        //获取传递过来的医院信息
//        Map<String, String[]> requestMap = request.getParameterMap();
//        Map<String, Object> paramsMap = HttpRequestHelper.switchMap(requestMap);
//
//        //获取医院传递过来的签名
//        String hospSign = (String) paramsMap.get("sign");
//
//        //根据医院编号来查询对应签名
//        String hoscode = (String)paramsMap.get("hoscode");
//        String signKey=hospitalSetService.getSignKey(hoscode);
//
//        //将查询数据也进行md5加密
//        String signKeyMd5 = MD5.encrypt(signKey);
//
//        //判断签名是否正确
//        if (!hospSign.equals(signKeyMd5)) {
//            //错误抛出异常
//            throw  new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }
        //替代上面的方式
        Map<String, Object> paramsMap = HospApiUtil.hospStringToObject(request, hospitalSetService);

        //将logoData进行base64解码,在传输过程中会把+号替换成空格，需要替换回来
        String logoData = (String)paramsMap.get("logoData");
        logoData=logoData.replaceAll(" ", "+");
        paramsMap.put("logoData", logoData);

        hospitalService.save(paramsMap);
        return Result.ok();
    }



}
